package com.nullpointer.xchatfinal.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.nullpointer.xchatfinal.R;
import com.nullpointer.xchatfinal.utils.Utils;

public class EditAvatarDialogFragment extends DialogFragment {
	private static final String FACEBOOK_ID = "FACEBOOK_ID";
	private TextView tvNotLoginFb;
	private Button btDisable;
	private Button btEnable;
	private Button btRefresh;
	private OnEditAvatarDialogFragment listener;
	private String facebookID;

	public static EditAvatarDialogFragment newInstance(String facebookID) {
		EditAvatarDialogFragment f = new EditAvatarDialogFragment();
		Bundle args = new Bundle();
		args.putString(FACEBOOK_ID, facebookID);
		f.setArguments(args);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		facebookID = getArguments().getString(FACEBOOK_ID);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_edit_avatar, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (Utils.isFacebookLoggedIn()) {
			tvNotLoginFb.setVisibility(View.GONE);
			if (facebookID == null) {
				btRefresh.setVisibility(View.GONE);
				btDisable.setVisibility(View.GONE);
				btEnable.setVisibility(View.VISIBLE);
			} else {
				btRefresh.setVisibility(View.VISIBLE);
				btDisable.setVisibility(View.VISIBLE);
				btEnable.setVisibility(View.GONE);
			}
		} else {
			tvNotLoginFb.setVisibility(View.VISIBLE);
			btDisable.setVisibility(View.GONE);
			btEnable.setVisibility(View.GONE);
			btRefresh.setVisibility(View.GONE);

			new CountDownTimer(5000, 1000) {

				@Override
				public void onTick(long millisUntilFinished) {

				}

				@Override
				public void onFinish() {
					dismiss();
				}
			}.start();
		}
	}

	private void setEvent() {
		btDisable.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (listener != null) {
					listener.onDisable();
				}
				dismiss();
			}
		});

		btEnable.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (listener != null) {
					listener.onEnable();
				}
				dismiss();
			}
		});

		btRefresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (listener != null) {
					listener.onRefresh();
				}
				dismiss();
			}
		});
	}

	private void findViewIds(View view) {
		tvNotLoginFb = (TextView) view.findViewById(R.id.tv_not_login_fb);
		btDisable = (Button) view.findViewById(R.id.bt_disable);
		btRefresh = (Button) view.findViewById(R.id.bt_refresh);
		btEnable = (Button) view.findViewById(R.id.bt_enable);
	}

	public void setOnEditAvatarDialogFragment(
			OnEditAvatarDialogFragment listener) {
		this.listener = listener;
	}

	public interface OnEditAvatarDialogFragment {
		void onRefresh();

		void onDisable();

		void onEnable();
	}
}
